// 转换时间戳
export function timeStamp(time) {
	let saveDate = new Date(parseInt(new Date(new Date(time).getTime()).getTime()))
	const year = saveDate.getFullYear() // 获取年份
	const month = String(saveDate.getMonth() + 1).padStart(2, '0') // 获取月份，并且补齐到两位数
	const day = String(saveDate.getDate()).padStart(2, '0') // 获取日期，并且补齐到两位数

	const hours = saveDate.getHours().toString().padStart(2, '0') // 获取小时，并且补齐到两位数
	const minutes = saveDate.getMinutes().toString().padStart(2, '0') // 获取分钟，并且补齐到两位数
	const seconds = saveDate.getSeconds().toString().padStart(2, '0') // 获取秒数，并且补齐到两位数

	return year + '.' + month + '.' + day + '  ' + hours + ':' + minutes + ':' + seconds
}
// 转换秒
export function getTime(time) {
	// 转换为时分秒
	let h = parseInt(time / 60 / 60 % 24)
	h = h < 10 ? '0' + h : h
	let m = parseInt(time / 60 % 60)
	m = m < 10 ? '0' + m : m
	let s = parseInt(time % 60)
	s = s < 10 ? '0' + s : s
	// 作为返回值返回
	return `${h}:${m}:${s}`
}
// 时间差

//相差时间计算
export function dateDiff(startDate, endDate) {
	const date1 = new Date(startDate);
	const date2 = new Date(endDate);

	const diffTime = Math.abs(date2.getTime() - date1.getTime());
	const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
	return diffDays
}
export function timeDiff(s1, s2) {
	var reDate = /\d{4}-\d{1,2}-\d{1,2} /;
	s1 = new Date((reDate.test(s1) ? s1 : '2018-1-1 ' + s1).replace(/-/g, '/'));
	s2 = new Date((reDate.test(s2) ? s2 : '2018-1-1 ' + s2).replace(/-/g, '/'));
	var ms = s2.getTime() - s1.getTime();
	if (ms < 0) return 0;
	return Math.floor(ms / 1000 / 60 / 60); //小时
}

// 获取去年的今天日期
export function getLastYear() {
	let date_format = new Date()
	let year = date_format.getFullYear() - 1
	let month = date_format.getMonth() + 1
	if (month < 10) month = '0' + month
	let date = date_format.getDate()
	if (date < 10) date = '0' + date
	let time = `${year}/${month}/${date}`
	return time
}
// 获取下个月的今天日期
export function getNextMonth() {
	let date_format = new Date()
	let year = date_format.getFullYear()
	let month = date_format.getMonth() + 2
	if (month < 10) month = '0' + month
	let date = date_format.getDate()
	if (date < 10) date = '0' + date
	let time = `${year}/${month}/${date}`
	return time
}
